﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;

namespace Helix_Network_Supervisor_3
{
    public partial class ScheduleItemForm : Form
    {
        #region Fields

        private List<int> dayList = new List<int>();
        private string[] itemList;
        private string ItemType;

        #endregion Fields

        #region Ctor

        public ScheduleItemForm()
        {
            InitializeComponent();
        }

        public ScheduleItemForm(string itemType, DateTime startTime, DateTime endTime)
        {
            InitializeComponent();
            this.Text = "Add " + itemType;

            ItemType = itemType;

            startTimeLabel.Text = itemType + " Start Time";
            endTimeLabel.Text = itemType + " End Time";

            EndTime = endTime;
            StartTime = startTime;
            
            StartDay = StartTime.Day;
        }

        public ScheduleItemForm(string itemType, string formTitle, DateTime startTime, DateTime endTime)
        {
            InitializeComponent();
            this.Text = formTitle;

            ItemType = itemType;

            startTimeLabel.Text = itemType + " Start Time";
            endTimeLabel.Text = itemType + " End Time";

            EndTime = endTime;
            StartTime = startTime;

            StartDay = StartTime.Day;

        }

        #endregion Ctor

        #region Properties

        public List<int> DayList
        {
            get
            {
                return dayList;
            }
        }

        public string ItemName
        {
            get
            {
                ListView.SelectedListViewItemCollection items = scheduleItemsListView.SelectedItems;
                if (items.Count > 0) return (items[0].SubItems[0].Text);
                else return ("");
            }
        }

        public string ItemPath
        {
            get
            {
                string itemName = ItemName;
                if (itemName != "")
                {
                    foreach (string name in itemList)
                    {
                        if (Path.GetFileNameWithoutExtension(name) == itemName)
                        {
                            return name;
                        }
                    }
                    return "";
                }
                else
                {
                    return "";
                }
            }
        }

        public DateTime StartTime
        {
            get
            {
                return itemStartTime.Value;
            }
            set
            {

                itemStartTime.Value = value;
            }
        }

        public DateTime EndTime
        {
            get
            {
                return itemEndTime.Value;
            }
            set
            {
                itemEndTime.Value = value;
            }

        }

        public int StartDay
        {
            set
            {
                switch (value)
                {
                    case 1:
                        {
                            sundayCheckBox.Checked = true;
                        }
                        break;

                    case 2:
                        {
                            mondayCheckBox.Checked = true;
                        }
                        break;

                    case 3:
                        {
                            tuesdayCheckBox.Checked = true;
                        }
                        break;

                    case 4:
                        {
                            wednesdayCheckBox.Checked = true;
                        }
                        break;

                    case 5:
                        {
                            thursdayCheckBox.Checked = true;
                        }
                        break;

                    case 6:
                        {
                            fridayCheckBox.Checked = true;
                        }
                        break;

                    case 7:
                        {
                            saturdayCheckBox.Checked = true;
                        }
                        break;
                }
            }
        }

        #endregion Properties

        #region Public Methods

        public void populateItemList(string itemPath, string columnTitle)
        {
            scheduleItemsListView.Columns.Add(columnTitle, 160, HorizontalAlignment.Left);
            if (Directory.Exists(itemPath))
            {
                itemList = Directory.GetFiles(itemPath);

                foreach (string file in itemList)
                {
                    ListViewItem item = new ListViewItem(Path.GetFileNameWithoutExtension(file));
                    scheduleItemsListView.Items.Add(item);
                }
            }
        }

        public void populateItemList(string itemPath, string columnTitle, string initialSelectedItem)
        {
            scheduleItemsListView.Columns.Add(columnTitle, 160, HorizontalAlignment.Left);
            if (Directory.Exists(itemPath))
            {
                itemList = Directory.GetFiles(itemPath);

                foreach (string file in itemList)
                {
                    ListViewItem item = new ListViewItem(Path.GetFileNameWithoutExtension(file));

                    if (item.Text == initialSelectedItem)
                    {
                        item.Selected = true;
                    }

                    scheduleItemsListView.Items.Add(item);
                }
            }
        }

        #endregion Public Methods

        #region Private Methods

        #endregion Private Methods

        #region Events

        private void everyDayCheckBox_CheckedChanged(object sender, EventArgs e)
        {
            if (everyDayCheckBox.Checked)
            {
                sundayCheckBox.Checked = true;
                mondayCheckBox.Checked = true;
                tuesdayCheckBox.Checked = true;
                wednesdayCheckBox.Checked = true;
                thursdayCheckBox.Checked = true;
                fridayCheckBox.Checked = true;
                saturdayCheckBox.Checked = true;
            }
            else
            {
                sundayCheckBox.Checked = false;
                mondayCheckBox.Checked = false;
                tuesdayCheckBox.Checked = false;
                wednesdayCheckBox.Checked = false;
                thursdayCheckBox.Checked = false;
                fridayCheckBox.Checked = false;
                saturdayCheckBox.Checked = false;
            }
        }

        private void weekdaysCheckBox_CheckedChanged(object sender, EventArgs e)
        {
            if (weekdaysCheckBox.Checked)
            {
                mondayCheckBox.Checked = true;
                tuesdayCheckBox.Checked = true;
                wednesdayCheckBox.Checked = true;
                thursdayCheckBox.Checked = true;
                fridayCheckBox.Checked = true;
            }
            else
            {
                mondayCheckBox.Checked = false;
                tuesdayCheckBox.Checked = false;
                wednesdayCheckBox.Checked = false;
                thursdayCheckBox.Checked = false;
                fridayCheckBox.Checked = false;
            }
        }

        private void weekendsCheckBox_CheckedChanged(object sender, EventArgs e)
        {
            if (weekendsCheckBox.Checked)
            {
                sundayCheckBox.Checked = true;
                saturdayCheckBox.Checked = true;
            }
            else
            {
                sundayCheckBox.Checked = false;
                saturdayCheckBox.Checked = false;
            }
        }

        private void scheduleItemsListView_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            this.DialogResult = DialogResult.OK;
            this.Close();
        }

        private void ScheduleItemForm_Shown(object sender, EventArgs e)
        {
            scheduleItemsListView.Focus();
        }

        private void itemStartTime_ValueChanged(object sender, EventArgs e)
        {
            if (itemStartTime.Value >= itemEndTime.Value)
            {
                if (itemStartTime.Value.Hour == 23 && itemStartTime.Value.Minute >= 30)
                {
                    DateTime tempDT = new DateTime(2010, 8, itemEndTime.Value.Day, 23, 59, 00);
                    EndTime = tempDT;
                }
                else
                {
                    TimeSpan ts = new TimeSpan(0, 30, 0);
                    EndTime = itemStartTime.Value.Add(ts);
                }
            }
        }

        private void ScheduleItemForm_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (this.DialogResult == DialogResult.Cancel)
            {
                return;
            }

            if (ItemName == "")
            {
                if (ItemType == "Show")
                {
                    MessageBox.Show("The Show Name was not selected.  Please try again.", "Helix Scheduler - Error",
                        MessageBoxButtons.OK, MessageBoxIcon.Stop);
                }
                else if (ItemType == "Playlist")
                {
                    MessageBox.Show("The Playlist Name was not selected.  Please try again.", "Helix Scheduler - Error",
                        MessageBoxButtons.OK, MessageBoxIcon.Stop);

                }
                else
                {
                    MessageBox.Show("The Show to be triggered was not selected.  Please try again.", "Helix Scheduler - Error",
                        MessageBoxButtons.OK, MessageBoxIcon.Stop);
                }

                e.Cancel = true;
            }
            else
            {
                // Get a list of the selected days.

                if (sundayCheckBox.Checked) dayList.Add(0);

                if (mondayCheckBox.Checked) dayList.Add(1);

                if (tuesdayCheckBox.Checked) dayList.Add(2);

                if (wednesdayCheckBox.Checked) dayList.Add(3);

                if (thursdayCheckBox.Checked) dayList.Add(4);

                if (fridayCheckBox.Checked) dayList.Add(5);

                if (saturdayCheckBox.Checked) dayList.Add(6);
            }

        }

        #endregion Events

    }
}
